import axios from "axios";
import baseURL from "./env.js";
const instance = axios.create({
  baseURL,
  timeout: 3000
});
instance.interceptors.request.use(
  function (config) {
    return Promise.resolve(config);
  },
  function (error) {
    return Promise.reject(error);
  }
);
instance.interceptors.response.use(
  function (response) {
    if (response.status == 200) {
      return Promise.resolve(response.data);
    }
    return Promise.reject({ msg: "请求失败" });
  },
  function (error) {
    return Promise.reject(error);
  }
);

export default {
  /**
   * @param {Object} url 接口地址
   * @param {Object} params 参数对象
   */
  get(url, params) {
    return instance.get(url, { params });
  },
  post(url, data) {
    return instance.post(url, data);
  },
  put(url, data) {
    return instance.put(url, data);
  },
  delete(url, id) {
    return instance.delete(url, { params: { id } });
  }
};
